import requests
from retrying import retry


class CalculatorApi:

    @retry(stop_max_attempt_number=3, wait_fixed=2000)
    # @staticmethod
    def get_grossProfitMargin(self,platformId,price,sku,country,calculationFormulaId):
        data = {
            "input": [
                {
                    "businessTypeCode": "AllegroLocalWarehouse",
                    "calculationFormulaId": calculationFormulaId,
                    "country": country,
                    "customerId": 1,
                    "platformId": platformId,
                    "price": price,
                    "sku": sku,
                    "warehouseId": 5312,
                    "warehouseTypeId": 1
                }
            ]
        }
        url = f"http://calculatorapi.zhcxkj.com/api/calculationFormula/calculationPrice/getCalculationGrossProfitMarginViewModelList"
        headers = {
            "Authorization": "Basic YXBpdXNlcjpFN0kxLWxJX2QjOGRscw=="
        }
        response = requests.post(url, headers=headers, json=data)
        if response.status_code == 200:
            if response.json()['status'] == "success":
                return response.json()['data'][0]['grossProfitMargin']
        else:
            raise
